package com.nf.practice.sortlist;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ArrayListTest1 {
    public static void main(String[] args) {
        List<Person1> list = new ArrayList<>();
        //产生20以内的随机数
        int num = (int)(Math.random()*20+1);
        for(int i=num;i>0;i--){
            list.add(new Person1(i,"张三","河南"));
        }
        for(Object o : list){
            System.out.println(o);
        }

        // 记忆方法：
        // 大于号 返回1，正乘正为正，所以升序(可以把>号想象为正)
        // 大于号返回-1，正乘负为负，所以降序


        System.out.println("==================================升序排列==================================");
        Collections.sort(list, new Comparator<Person1>() {
           @Override
           public int compare(Person1 o1, Person1 o2) {
               if (o1.getId() > o2.getId()) {
                   return 1;
               } else if (o1.getId() < o2.getId()) {
                   return -1;
               }
               return 0;
           }
 
       });
        for(Object o : list){
            System.out.println(o);
        }

        long count = 0;
        count = list.stream().filter(w -> w.getId() > 5).count();
        System.out.println("id大于5的计数为：" + count);


        System.out.println("==================================降序排列==================================");
        Collections.sort(list, new Comparator<Person1>() {
            @Override
            public int compare(Person1 o1, Person1 o2) {
                if (o1.getId() > o2.getId()) {
                    return -1;
                } else if (o1.getId() < o2.getId()) {
                    return 1;
                }
                return 0;
            }

        });
        for(Object o : list){
            System.out.println(o);
        }
    }
}